package com.mrjy.video.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.CustomModule;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.VideoInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.IMessageInfoDao;
import com.mrjy.maintain.dao.ISubscriptionInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.util.RoleInfoEnum;
import com.mrjy.video.bo.IVideoInfoBo;

@Controller
@RequestMapping("/video/videoInfo")
public class VideoInfoController {
	@Resource
	private IVideoInfoBo videoInfoBo;
    @Resource
    private IUserInfoDao userInfoDao;
    @Resource
    private ISubscriptionInfoDao subscriptionInfoDao;
    @Resource
    private IMessageInfoDao messageInfoDao;

    /**
     * @author Javin
	 * @date 2017年9月13日
	 * @desc 添加视频 for admin
	 * @param 
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
    public Object insert(VideoInfo videoInfo, HttpSession session) {
        Map<String, Object> map = (Map<String, Object>) videoInfoBo.txInsert(videoInfo, session);
        return map;
    }

	@RequestMapping("/deleteByIds")
	@ResponseBody
	public Object deleteByIds(int[] ids, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (userInfo.getRoleId().intValue() == RoleInfoEnum.SYS_ADMIN.getRoleId() 
				|| userInfo.getRoleId().intValue() == RoleInfoEnum.VIDEO_ADMIN.getRoleId()) {
			return videoInfoBo.txDeleteByIds(ids);
		} else {
			return "Forbidden";
		}
	}
	/**
	 * @author Javin
	 * @date 2017年9月15日
	 * @desc 启用禁用-支持批量 for admin
	 * @param videoInfo
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(VideoInfo videoInfo,HttpSession session) {
		return videoInfoBo.txUpdateDisableFlag(videoInfo,session);
	}
	
	/**
	 * @author Javin
	 * @date 2017年9月15日
	 * @desc 编辑视频 for admin
	 * @param videoInfo
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public Object updateById(VideoInfo videoInfo,HttpSession session) {
		return videoInfoBo.txUpdateById(videoInfo,session);
	}

	@RequestMapping("/queryById")
	@ResponseBody
	public Object queryById(int id) {
		return videoInfoBo.queryById(id);
	}

	@RequestMapping("/queryList")
	@ResponseBody
	public Object queryList(VideoInfo videoInfo) {
		return videoInfoBo.queryList(videoInfo);
	}
	/**
	 * 根据讲师id userId 获取讲师的所有视频
	 * @param videoInfo videoInfo.userId videoInfo.page videoInfo.rows
	 * @author csshui isUse=true
	 * @return
	 */
	@RequestMapping("/queryPage")
	@ResponseBody
	@Deprecated
	public Object queryPage(VideoInfo videoInfo,HttpSession session) {
		return videoInfoBo.queryPage(videoInfo,session);
	}
	/**
	 * 通过视频id 获取视频的相关信息及播放url地址
	 * @param videoId 视频id
	 * @param session
	 * @author csshui isUse=true
	 * @return
	 */
	@RequestMapping("/queryUrlByVideoId")
	@ResponseBody
	public Object queryUrlByVid(Integer videoId, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (videoId != null) {
			return videoInfoBo.queryUrlByVideoId(videoId, userInfo != null ? userInfo.getId() : null);
		} else {
			return "error";
		}
	}

	@RequestMapping("/queryVideoList")
	@ResponseBody
	public Object queryVideoList(VideoInfo videoInfo, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		videoInfo.setUserId(userInfo != null ? userInfo.getId() : null);
		return videoInfoBo.queryVideoList(videoInfo);
	}
	/**
	 * 查询某个分类的所有视频
	 * @param videoInfo videoInfo.typeId(视频类型id) videoInfo.gradeId（年级id） videoInfo.freeFlag（免费标识） videoInfo.hotFlag（热度标识） videoInfo.page videoInfo.rows
	 * @param session
	 * @author csshui isUse=true
	 * @return
	 */
	@RequestMapping("/queryVideoPage")
	@ResponseBody
	public Object queryVideoPage(VideoInfo videoInfo, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		videoInfo.setUserId(userInfo != null ? userInfo.getId() : null);
		return videoInfoBo.queryVideoPage(videoInfo);
	}
	/**
	 * 微信1.5
	 * 通过热门标签查询视频列表 
	 * @author csshui isUse=true
	 * @return
	 */
	@RequestMapping("/queryVideoListByFlag")
	@ResponseBody
	public Object queryVideoListByFlag(VideoInfo videoInfo, HttpSession session) {		
//		if (!(new Integer(0)).equals(videoInfo.getUserId())) {
//			UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//			videoInfo.setUserId(userInfo != null ? userInfo.getId() : null);
//		}else{
//			videoInfo.setUserId(null);
//		}
        return videoInfoBo.queryVideoListByFlag(videoInfo);
    }

    /**
	 * 通过免费标签查询视频列表
	 * @param videoInfo  videoInfo.page videoInfo.rows videoInfo.freeFlag(0：否，即付费；1：免费) videoInfo.userId
	 * @param session
	 * @return
	 */
	@RequestMapping("/queryVideoPageByFlag")
	@ResponseBody
	public Object queryVideoPageByFlag(VideoInfo videoInfo, HttpSession session) {
		if (!(new Integer(0)).equals(videoInfo.getUserId())) {
			UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
			videoInfo.setUserId(userInfo != null ? userInfo.getId() : null);
		}else{
			videoInfo.setUserId(null);
		}
		return videoInfoBo.queryVideoPageByFlag(videoInfo);
	}
	
	/**
	 * 分页查询最新,最热课程
	 * @param videoInfo
	 * @return
	 * @author Javin :isUer = true, isNew = true
	 */
	@RequestMapping("/queryVideoPageDesc")
	@ResponseBody
	public Object queryVideoPageDesc(VideoInfo videoInfo){
		return videoInfoBo.queryVideoPageDesc(videoInfo);
	}
	
	/**
	 * 分页根据标签名查询视频
	 * @param videoInfo
	 * @return
	 * @author Javin : isUer =true, isNew = true
	 */


	@RequestMapping("/queryVideoPageByLableName")
	@ResponseBody
	public Object queryVideoPageByLableName(VideoInfo videoInfo){
        return videoInfoBo.queryVideoPageByLableName(videoInfo);
	}
	
	/**
	 * 根据视频ID查询视频信息
	 * @param videoInfo
	 * @return
	 */
	@RequestMapping("/queryVideoInfoById")
	@ResponseBody
    public Object queryVideoInfoById(VideoInfo videoInfo, HttpSession session) {
        return videoInfoBo.queryVideoInfoById(videoInfo, session);
    }
	
	/**
	 * 根据老师ID分页查询视频list
	 * @param info
	 * @return
	 */
	@RequestMapping("/getVideoListByTeacherId")
	@ResponseBody
    public Object getVideoListByTeacherId(VideoInfo info, HttpSession session) {
        return videoInfoBo.getVideoListByTeacherId(info, session);
    }
	
	/**
	 * 根据视频名称模糊分页收索
	 * @param info
	 * @return
	 * @author Javin : isUer = true isNew = true;
	 */
	@RequestMapping("/getPageVideoByVideoName")
	@ResponseBody
	public Object getPageVideoByVideoName(VideoInfo info){
		return videoInfoBo.getPageVideoByVideoName(info);
	}
	
	/**
	 * 根据typeName查询 记录数
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryVideoCountByLableName")
	@ResponseBody
	public Object queryVideoCountByLableName(VideoInfo info , HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo != null){
			info.setUserId(userInfo.getId());
		}
		return videoInfoBo.queryVideoCountByLableName(info);
	}
	
	/**
	 * 根据id 更新播放次数(观看60秒后才记录)
	 * @param vid
	 * @return
	 */
	@RequestMapping("/addPlayTimes")
	@ResponseBody
	public Object addPlayTimes(Integer vid){
		return videoInfoBo.txAddPlayTimes(vid);
	}
	
	/**
	 * 根据id 更新点击量(点开视频就记录)
	 * @param vid
	 * @return
	 */
	@RequestMapping("/addClickCount")
	@ResponseBody
	public Object addClickCount(Integer vid){
		return videoInfoBo.txAddClickCount(vid);
	}
	
	/**
	 * 根据视频播放数，点赞数，评论数分页查询
	 * @param info
	 * @return
	 * @author Javin
	 */
	@RequestMapping("/queryPageByPlayTimes")
	@ResponseBody
	public Object queryPageByViewNum(VideoInfo info){
		return videoInfoBo.queryPageByPlayTimes(info);
	}
	
	/**
	 * 分页查询免费视频
	 * @param info
	 * @return
	 * @author Javin
	 */
	@RequestMapping("/queryPageByFree")
	@ResponseBody
	public Object queryPageByFree(VideoInfo info){
		return videoInfoBo.queryPageByFree(info);
	}
	
	@RequestMapping("/selectPageForBackCoursePackage")
	@ResponseBody
	public Object selectPageForBackCoursePackage(VideoInfo info){
		return videoInfoBo.selectPageForBackCoursePackage(info);
	}
	
	/**
	 * @author Javin
	 * @date 2017年9月14日
	 * @desc 查询视频列表 for admin
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryPageVideo")
	@ResponseBody
	public Object queryPageVideo(VideoInfo info,HttpSession session){
		return videoInfoBo.queryPageVideo(info,session);
	}
	
	/**
	 * 导出
	 * @param dto
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(VideoInfo info,HttpServletResponse response,HttpSession session){
		return videoInfoBo.exportExl(info,response,session);
	}
	
	
	/**
	 * 微信1.5
	 * 更新视频的完成状态  isFinish(0--未看完  1--已看完)
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateFinishStatus")
	@ResponseBody
	public Object updateFinishStatus(VideoInfo info){
		return videoInfoBo.updateFinishStatus(info);
	}


    /**
     * 获取单个视频信息
     *
     * @return
     */
    @RequestMapping("/queryVideoMsgByVids")
    @ResponseBody
    public Object queryVideoMsgByVids(VideoInfo info) {
        return videoInfoBo.queryVideoMsgByVids(info);
    }


    /**
     * 猜你喜欢
     *
     * @return
     */
    @RequestMapping("/queryListForYourFavor")
    @ResponseBody
    public Object queryListForYourFavor(VideoInfo info) {
        return videoInfoBo.queryListForYourFavor(info);
    }

    /**
     * 显示用户未看的包含ids标签的微课
     *
     * @return
     */
    @RequestMapping("/queryListForMultiVideoType")
    @ResponseBody
    public Object queryListForMultiVideoType(CustomModule info) {
        return videoInfoBo.queryListForMultiVideoType(info);
    }

    /**
     * 分页查询最新,最热课程 根据老师
     *
     * @return
     */
    @RequestMapping("/queryVideoPageDescByUserId")
    @ResponseBody
    public Object queryVideoPageDescByUserId(VideoInfo info, HttpSession session) {
        return videoInfoBo.queryVideoPageDescByUserId(info, session);
    }

}
